package service

import (
	"context"
	"fmt"
	shell "github.com/ipfs/go-ipfs-api"
	"os"
	"ruoyi-go/app/model"
	"ruoyi-go/app/query"
	"ruoyi-go/app/token"
	"strings"
)

type FileService struct{}

func (service *FileService) UploadPathFile(path string) {

	file, err := os.Open(path)
	sh := shell.NewShell("localhost:5001")
	if err != nil {
		return
	}
	defer file.Close()
	sh.Add(file)

}

func (service *FileService) UploadStr() {
	sh := shell.NewShell("localhost:5001")
	cid, err := sh.Add(strings.NewReader("hello world"))
	if err != nil {
		fmt.Println("upload err:", err)
	}
	fmt.Println(cid)
	fmt.Print("Start to test upload File End")
}

func (service *FileService) SaveFileData(authUser *token.UserTokenResponse, key string, name string) {
	q := query.QueryI
	do := q.FileMsg.WithContext(context.Background())
	msg := model.FileMsg{
		UserID:         string(authUser.UserId),
		FileOriginName: name,
		FileName:       key[strings.LastIndex(key, "/")+1:],
		FilePath:       key,
	}
	do.Save(&msg)
}

func (service *FileService) GetFileData(id int32) *model.FileMsg {
	q := query.QueryI
	msg, err := q.FileMsg.FileMsgDo.Where(q.FileMsg.ID.Eq(id)).First()
	if err != nil {
		fmt.Println(err)
	}

	return msg
}
